<?php

namespace App\Admin\Actions\NetworkGoods;

use App\Models\DataDictionaryModel;
use App\Models\yb\NetworkGoodsModel;
use App\Models\yb\NetworkGoodsReviewModel;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;


class GoodsAudit extends RowAction
{
    public $name = '审核';

    /**
     * @param Model $model
     * @return string
     */
    public function handle(Model $model, Request $request)
    {
        $type = $request->get('type');

        $content = $request->get('content');
        if($type == 1){
            $status = 1;
        }else{
            $status = 0;
        }

        NetworkGoodsModel::where(['id'=>$model->id])->update([
            'review_status' => $status
        ]);

        NetworkGoodsReviewModel::insert([
            'network_id' => $model->network_id,
            'goods_id' => $model->goods_id,
            'network_goods_id' => $model->id,
            'created_at' => date('Y-m-d H:i:s',time()),
            'content' => $content,
            'status'=>$type,
            'status_name'=>DataDictionaryModel::LossReportStatus[$type],
        ]);

        return $this->response()->success('操作成功')->refresh();

    }

    /**
     * @param $star
     * @return string
     */
    public function display($id)
    {
        return  '<button type="button" class="btn btn-xs btn-info btn-flat">审核</button>';
    }


    public function form()
    {
        $this->radio('type', '类型')->options([1 => '通过',2=>'驳回'])->required()->default(1);

        $this->textarea('content','备注');

    }

}